﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace RMSM
{
	[Serializable()]
	public class HopDongBOCollection : Csla.BusinessListBase<HopDongBOCollection, HopDongBO>
	{
		#region BindingList Overrides
		protected override object AddNewCore()
		{
			HopDongBO _HopDongBO = HopDongBO.NewChildHopDongBO();
			_HopDongBO.STT0 = this.Count + 1;
			OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_HopDongBO));

			Add(_HopDongBO);
			return _HopDongBO;
		}
		#endregion BindingList Overrides
		//==========================================================================

		#region Factory Methods
		private HopDongBOCollection()
		{
		}

		public static HopDongBOCollection NewHopDongBOCollection()
		{
			return new HopDongBOCollection();
		}

		public static HopDongBO NewHopDongBO()
		{
			return HopDongBO.NewChildHopDongBO();
		}

		public static HopDongBOCollection GetHopDongBOCollection()
		{
			return GetHopDongBOCollection(null, null, null, false);
		}

		public static HopDongBOCollection GetHopDongBOCollection(bool getDeleted)
		{
			return GetHopDongBOCollection(null, null, null, getDeleted);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where)
		{
			return GetHopDongBOCollection(where, null, null, false);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where, bool getDeleted)
		{
			return GetHopDongBOCollection(where, null, null, getDeleted);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where, string orderCol)
		{
			return GetHopDongBOCollection(where, orderCol, null, false);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where, string orderCol, bool getDeleted)
		{
			return GetHopDongBOCollection(where, orderCol, null, getDeleted);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where, string orderCol, string orderExp)
		{
			return GetHopDongBOCollection(where, orderCol, orderExp, false);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where, string orderCol, string orderExp, bool getDeleted)
		{
			return GetHopDongBOCollection(where, orderCol, orderExp, getDeleted, false);
		}

		public static HopDongBOCollection GetHopDongBOCollection(string where, string orderCol, string orderExp, bool getDeleted, bool hasOtherParam)
		{
			string swhere = "";
			if (getDeleted == false)
				swhere += "WHERE DELE='0'" + (string.IsNullOrEmpty(where) ? "" : " AND " + where);
			else
				swhere = where;

			SPParam spParam = new SPParam((string.IsNullOrEmpty(orderCol) ? "HOPDONGID" : orderCol), (string.IsNullOrEmpty(orderExp) ? "ASC" : orderExp), swhere);
			if (hasOtherParam)
				spParam.otherParam = "ABC";

			return DataPortal.Fetch<HopDongBOCollection>(spParam);
		}
		#endregion Factory Methods
		//==========================================================================

		#region Criteria
		[Serializable()]
		private class Criteria
		{
			public Criteria()
			{
			}
		}

		[Serializable()]
		public class FilterCriteria
		{
			public FilterCriteria()
			{
			}
		}
		#endregion Criteria
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		private void DataPortal_Fetch(SPParam spParam)
		{
			RaiseListChangedEvents = false;
			using (FbConnection conn = new FbConnection(Session.CnnStr))
			{
				conn.Open();
				using (FbCommand cmd = conn.CreateCommand())
				{
					if (spParam.otherParam == null)
					{
						cmd.CommandText = "HopDongBO_C";
						cmd.CommandType = CommandType.StoredProcedure;
						cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = spParam.whereExpression;
						cmd.Parameters.Add("@ORDERCOL", FbDbType.VarChar).Value = spParam.orderColumn;
						cmd.Parameters.Add("@ORDEREXP", FbDbType.VarChar).Value = spParam.orderExpression;
						cmd.Parameters.Add("@IFIRST", FbDbType.VarChar).Value = Session.RowCount.ToString();
						cmd.Parameters.Add("@ISKIP", FbDbType.VarChar).Value = ((Session.PageIndex - 1) * Session.RowCount).ToString();
					}
					else
					{
						cmd.CommandText = "HopDongBO_C2";
						cmd.CommandType = CommandType.StoredProcedure;
						cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = spParam.whereExpression;
						cmd.Parameters.Add("@ORDERCOL", FbDbType.VarChar).Value = spParam.orderColumn;
						cmd.Parameters.Add("@ORDEREXP", FbDbType.VarChar).Value = spParam.orderExpression;
					}

					using (FbDataReader reader = cmd.ExecuteReader())
					{
						int stt = 0;
						while (reader.Read())
						{
							HopDongBO hopDongBO = HopDongBO.GetChildHopDongBO(reader);
							hopDongBO.STT0 = ++stt;
							this.Add(hopDongBO);
						}
					}
				}
			}
			RaiseListChangedEvents = true;
		}
		#endregion Fetch
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			Update();
		}

		internal void Update()
		{
			RaiseListChangedEvents = false;
			HopDongBO child = null;
			int length = this.Count;
			bool del = false;
			for (int i = 0; i < length; i++)
			{
				child = this[i];
				del = StaticUtil.GetBool(child.DELE);

				if (child.IsNew && !del)
					child.Insert();
				else
					child.Update();

				if (del)
				{
					this.RemoveAt(i);
					i--;
					length--;
				}
			}
			RaiseListChangedEvents = true;
		}
		#endregion Update
		//==========================================================================

		#region Business Method
		public HopDongBO GetBO(int? HopDongID)
		{
			if (HopDongID == null)
				return null;

			int length = this.Count;
			for (int i = 0; i < length; i++)
				if (this[i].HopDongID == HopDongID)
					return this[i];

			return null;
		}

		public int GetIndex(int? HopDongID)
		{
			if (HopDongID == null)
				return -1;

			int length = this.Count;
			for (int i = 0; i < length; i++)
				if (this[i].HopDongID == HopDongID)
					return i;

			return -1;
		}

		public void Remove(int? HopDongID)
		{
			if (HopDongID == null)
				return;

			int length = this.Count;
			for (int i = 0; i < length; i++)
			{
				if (this[i].HopDongID == HopDongID)
				{
					try
					{
						this.RemoveAt(i);
					}
					catch { }

					break;
				}
			}
		}
		#endregion Business Method
		//==========================================================================
	}
}